//
//  XMShareManager.m
//  SmallDay
//
//  Created by new-1020 on 2017/4/25.
//  Copyright © 2017年 com.xiaorizi. All rights reserved.
//

#import "XMShareManager.h"

//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <ShareSDKExtension/ShareSDK+Extension.h>
#import "NSDictionary+Property.h"
//微信SDK头文件
#import "WXApi.h"
//新浪微博SDK头文件
#import "WeiboSDK.h"
#import "UserTool.h"
@implementation XMShareManager

+(void)registerShare {
        [ShareSDK registerApp:ShareSDKAppKey
              activePlatforms:@[
                                @(SSDKPlatformTypeSinaWeibo),
                                @(SSDKPlatformTypeWechat),
                                @(SSDKPlatformTypeQQ)]
                     onImport:^(SSDKPlatformType platformType)
         {
             switch (platformType)
             {
                 case SSDKPlatformTypeWechat:
                     [ShareSDKConnector connectWeChat:[WXApi class]];
                     break;
                 case SSDKPlatformTypeQQ:
                     [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                     break;
                 case SSDKPlatformTypeSinaWeibo:
                     [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                     break;
                 default:
                     break;
             }
         }
              onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
         {
             
             switch (platformType)
             {
                 case SSDKPlatformTypeSinaWeibo:
                     //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                     [appInfo SSDKSetupSinaWeiboByAppKey:@"989495433"
                                               appSecret:@"926cb6d818fdba436d1d38601ae63284"
                                             redirectUri:@"http://www.xiaorizi.me"
                                                authType:SSDKAuthTypeBoth];
                     break;
                 case SSDKPlatformTypeWechat:
                     [appInfo SSDKSetupWeChatByAppId:@"wx4187d3bacd59b7e0"
                                           appSecret:@"44309f9fda796b2b4bc33b5923ccc4f4"];
                     break;
                 case SSDKPlatformTypeQQ:
                     [appInfo SSDKSetupQQByAppId:@"1104435229"
                                          appKey:@"b8rjC0YJjluWjRke"
                                        authType:SSDKAuthTypeBoth];
                     break;
                 default:
                     break;
             }
         }];
}

+(BOOL)CanWechatLogin {
    NSLog(@"%d", [ShareSDK isClientInstalled:SSDKPlatformTypeWechat]);
    return [ShareSDK isClientInstalled:SSDKPlatformTypeWechat];
}

+(BOOL)CanQQLogin {
    return [ShareSDK isClientInstalled:SSDKPlatformTypeQQ];
}

+(void)doLongin:(LoginType)type loginSuccessBlock:(void(^)(id))Successblock {
    [Apputil showMessage:@"登录中..."];
    SSDKPlatformType platType;
    switch (type) {
        case QQLogin:
        {
            platType = SSDKPlatformTypeQQ;
        }
            break;
        case WeiBoLogin:
        {
            platType = SSDKPlatformTypeSinaWeibo;
        }
            break;
        case WexinLogin:
        {
            platType = SSDKPlatformTypeWechat;
        }
            break;
        default:
            break;
    }
    
    [ShareSDK getUserInfo:platType
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
//             typedef enum{
//                 LOGINTYPE_PHONE   = 0,
//                 LOGINTYPE_WEIXIN  = 1,
//                 LOGINTYPE_QQ      = 2,
//                 LOGINTYPE_WEIBO   = 3,
//             }LOGINTYPE;
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
             NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
             NSLog(@"%@",user.rawData);
             dic[@"login_type"] = @(type);
             dic[@"open_id"] = user.uid;
             dic[@"name"] = user.nickname;
             if (user.gender == SSDKGenderMale ) {
                 dic[@"sex"] = @"1";
             }else {
                 dic[@"sex"] = @"0";
             }
             // 微博参数
             if (type == WeiBoLogin) {
                 dic[@"province"] = user.rawData[@"province"];
                 dic[@"head_photo"] = user.rawData[@"avatar_large"];
                 dic[@"city"] = user.rawData[@"city"];
             }
             // 微信参数
             if (type == WexinLogin) {
                 dic[@"province"] = user.rawData[@"province"];
                 dic[@"head_photo"] = user.rawData[@"headimgurl"];
                 dic[@"city"] = user.rawData[@"city"];
             }
             if (type == QQLogin) {
                 dic[@"province"] = user.rawData[@"province"];
                 dic[@"head_photo"] = user.rawData[@"figureurl_qq_2"];
                 dic[@"city"] = user.rawData[@"city"];
             }
             if (Successblock) {
                 Successblock(dic);
             }
         }
         else
         {
             NSLog(@"登录错误%@",error);
             [Apputil showError:@"登录失败"];
         }
         
     }];
}

+ (void)loginWithPhone:(NSString *)phonemun password:(NSString *)pwd loginSuccessBlock:(void (^)(void))Successblock
{
    
}

+ (void)shareShow {
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"1.jpeg"]];
//    （注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://mob.com"]
                                          title:@"分享标题"
                                           type:SSDKContentTypeAuto];
        //有的平台要客户端分享需要加此方法，例如微博
        [shareParams SSDKEnableUseClientShare];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
 
        [ShareSDK showShareActionSheet:nil
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                   message:nil
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"确定"
                                                                         otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           default:
                               break;
                       }
                   }
         ];}
}


+(void)customItemWithtype:(SSDKPlatformType)type{
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage new]];
    //    NSArray* imageArray = @[@"test"];
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if (imageArray) {
        [shareParams SSDKSetupShareParamsByText: @"分享吧"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://baidu.com"]
                                          title:@"大意思"
                                           type:SSDKContentTypeAuto];
    }
    
    [ShareSDK share:type //传入分享的平台类型
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
         switch (state) {
             case SSDKResponseStateSuccess:
             {
                 if (type==SSDKPlatformTypeSinaWeibo) {
                     
                     [Apputil showError:@"微博分享成功"];
                 }else{
                     [Apputil showMessage:@"分享成功！" afterDelay:1.0];
                 }
                 break;
             }
             case SSDKResponseStateFail:
             {
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                 message:[NSString stringWithFormat:@"%@",error]
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:nil, nil];
                 [alert show];
                 break;
             }
             default:
                 break;
         }
     }];
}
@end
